<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\AdminNode as AdminNodeModel;
use think\Db;

class AdminNode extends Controller
{
    // 权限节点列表
    public function index()
    {   
        $res = AdminNodeModel::where(['level' => 1])->select();
        return $this->assign('res',$res)->fetch('adminnode/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch('adminnode/add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 依赖注入的方式
        $post = $request->post();
        // 实例化Model
        $adminRole = new AdminNodeModel();
        // 传入数据到Model中
        $adminRole->data($post);
        $adminRole->level = 1;
        // 过滤非数据表的字段,并写入数据, 返回写入记录数量
        $result = $adminRole->allowField(true)->save();
        // 判断跳转
        if ($result > 0) {
            return $this->success('添加成功了', url('admin/admin_node/index'));
        } else {
            return $this->error('添加失败了');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = AdminNodeModel::get($id)->getData();
        return $this->assign('res',$res)->fetch('adminnode/edit');
    }


    // 权限节点控制器更新
    public function update(Request $request, $id)
    {
        $post = $request->post();
        $data = [
            'name'=> $post['name'],
            'title'=> $post['title'],
            'status'=> $post['status'],
            'sort'=> $post['sort'],
            'remark'=> $post['remark']
        ];
        $res = AdminNodeModel::where(['id' => $id])->update($data);
        if ($res) { 
            return $this->success('修改成功了', url('admin/admin_node/index'));
        } else {
            return $this->error('修改失败了');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }


    // 控制器下 方法 列表
    public function method($pid)
    {
        $res = AdminNodeModel::where(['level' => 2, 'pid' => $pid ])->select();
        $this->assign('pid',$pid);
        return $this->assign('res',$res)->fetch('adminnode/method');
    }

    // 编辑方法
    public function editMethod($id)
    {
        $res = AdminNodeModel::get($id)->getData();
        return $this->assign('res',$res)->fetch('adminnode/editmethod');
    }

    // 更新方法
    public function updateMethod($id)
    {
        $data = [
            'name'=> input('post.name'),
            'title'=> input('post.title'),
            'status'=> input('post.status'),
            'sort'=> input('post.sort'),
            'remark'=> input('post.remark')
        ];
        $res = AdminNodeModel::where(['id' => $id])->update($data);
        if ($res) { 
            return $this->success('修改成功了', url('admin/admin_node/index'));
        } else {
            return $this->error('修改失败了');
        }
    }

     // 添加方法
    public function createMethod($pid)
    {
        $this->assign('pid',$pid);
        return $this->fetch('adminnode/addmethod');
    }

     // 添加方法
    public function saveMethod($pid)
    {
        $data = [
            'pid' => $pid,
            'name'=> input('post.name'),
            'title'=> input('post.title'),
            'status'=> input('post.status'),
            'sort'=> input('post.sort'),
            'level'=> 2,
            'remark'=> input('post.remark')
        ];
        $res = AdminNodeModel::insert($data);
        if ($res) { 
            return $this->success('方法添加成功', url('admin/admin_node/index'));
        } else {
            return $this->error('方法添加失败');
        }
    }

}
